﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_Asp_UserControls_ThumbnailSelection : System.Web.UI.UserControl
{
    string[] m_allowedExtensions = new string[] { ".jpg", ".jpeg" ,".png" };
    string m_fileExtension = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        x_upload_error.Visible = false;
        x_selectionList.DataSource = ModelItemHandler.GetThumbnailList();
        x_selectionList.DataBind();
    }    

    protected void selectionList_Changed(object sender, EventArgs e)
    {
        x_selectionList.DataBind();
        DataListItem f_item = x_selectionList.SelectedItem;

        //So hardcoded substringing to remove ~/
        x_itemInfo_filename.Text = (f_item.FindControl("x_selectedItemImage") as Image).ImageUrl.Substring(2);
    }

    protected void selectionList_ItemCommand(object source, DataListCommandEventArgs e)
    {
        DataList dl = source as DataList;

        int selIdx = dl.SelectedIndex;

        switch (e.CommandName.ToLower())
        {
            case "select":
                selIdx = e.Item.ItemIndex;
                x_itemInfoPanel.Visible = true;
                break;
            case "unselect":
                selIdx = -1;
                x_itemInfoPanel.Visible = false;
                break;
        }

        if (selIdx != dl.SelectedIndex)
            dl.SelectedIndex = selIdx;
        dl.DataBind();
    }

    protected void CloseSelection(object sender, EventArgs e)
    {
        x_selectionList.SelectedIndex = -1;
        x_upload_error.Text = "";
        x_upload_filename.Text = "";
        this.Visible = false;
    }

    /// <summary>
    /// When user clicks on the Update button to send the changes to ModelEditHandler.asxc
    /// </summary>    
    protected void UpdateTexture(object sender, EventArgs e)
    {
        try
        {
            Session[Strings.Session_Thumbnail] = x_itemInfo_filename.Text;

            string f_id = (Request.QueryString[Strings.Query_Edit_ID] == null) ? "" : "?" + Strings.Query_Edit_ID + "=" + Request.QueryString[Strings.Query_Edit_ID];
            Response.Redirect(Request.Path + f_id);
            //Server.Transfer(Request.RawUrl);
        }
        catch
        {

        }
    }

    protected void TryToUpload(object sender, EventArgs e)
    {
        bool f_validated = true;

        bool f_extensionOK = false;

        string f_filename = x_upload_filename.Text.ToLower();

        for (int i = 0; i < m_allowedExtensions.Length; i++)
        {
            if (f_filename.EndsWith(m_allowedExtensions[i]))
            {
                f_extensionOK = true;
                m_fileExtension = m_allowedExtensions[i];
                break;
            }
        }

        if (!x_upload_filename.Text.EndsWith(".jpg") && !x_upload_filename.Text.EndsWith(".jpeg"))
        {
            x_upload_filename.Text = x_upload_filename.Text + ".jpg";
        }

        //Check file extension on uploaded file and output texture, also checks that 
        if (!f_extensionOK)
        {
            x_upload_error.Text = "File needs to be .jpg or .png";
            f_validated = false;

        }
        else if (x_upload_filename.Text == "")
        {
            x_upload_error.Text = "You need to specify a filename";
            f_validated = false;
        }
        else if (x_upload_filename.Text.Contains("..") || x_upload_filename.Text.StartsWith(".") || x_upload_filename.Text.StartsWith("/") || x_upload_filename.Text.StartsWith("\\"))
        {
            x_upload_error.Text = "Output filename's path is not a valid one. Make sure it doesn't contain ../ or starts with ./\\";
            f_validated = false;
        }

        if (f_validated)
        {
            //string f_filename = x_upload_filename.Text.ToLower();
            if (ModelItemHandler.CreateThumbnail(x_upload_file, f_filename, Paths.thumbnailFolder))
            {
                //If it uploads succesfully then add the file to the datalist
                DataView f_dv = x_selectionList.DataSource as DataView;
                DataRow f_dr = f_dv.Table.NewRow();

                f_dr[0] = f_filename;
                f_dv.Table.Rows.Add(f_dr);

                x_selectionList.DataBind();
                //Set the output text to ""
                x_upload_filename.Text = "";
            }
            else
            {
                x_upload_error.Text = "Failed to save file";
                x_upload_error.Visible = true;
            }
        }
        else
        {
            x_upload_error.Visible = true;
        }

    }
}